package logic

import (
	"context"
	"edu/edurpc/edurpc"
	"edu/edurpc/internal/svc"
	"edu/models/mysql"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelAddLogic {
	return &NovelAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelAddLogic) NovelAdd(in *edurpc.NovelAddRequest) (*edurpc.NovelAddResponse, error) {
	if in.Title == "" || in.Category == "" || in.Refer == "" {
		return nil, fmt.Errorf("标题,分类,简介不能为空")
	}
	var user mysql.User
	mysql.DB.Where("id = ?", in.UserId).First(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("用户不存在")
	}
	novel := mysql.Novel{
		UserId:   int(in.UserId),
		Title:    in.Title,
		Category: in.Category,
		Info:     in.Info,
		Refer:    in.Refer,
		Image:    in.Image,
	}
	mysql.DB.Create(&novel)
	return &edurpc.NovelAddResponse{
		NovelId: int64(novel.ID),
	}, nil
}
